package lanou.giftsay_01.fragment.categorybody;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import lanou.giftsay_01.R;
import lanou.giftsay_01.adapter.LeftAdapter;
import lanou.giftsay_01.adapter.RightAdapter;
import lanou.giftsay_01.bean.DanpinBean;
import lanou.giftsay_01.tools.BaseFragment;
import lanou.giftsay_01.tools.NetTool;
import lanou.giftsay_01.tools.URLValues;

/**
 * Created by dllo on 16/8/18.
 */
public class DanPinFragment extends BaseFragment {

    private LeftAdapter leftAdapter;
    private RightAdapter rightAdapter;
    private ListView leftLv;
    private ListView rightLv;
    private int lastPosition;

    @Override
    protected int setLayout() {
        return R.layout.fragment_danpin;
    }


    @Override
    protected void initView() {
        leftLv = (ListView) getView().findViewById(R.id.left_lv);
        rightLv = (ListView) getView().findViewById(R.id.right_lv);


    }

    @Override
    protected void initData() {
        leftAdapter = new LeftAdapter(mContext);
        rightAdapter = new RightAdapter(mContext);
        NetTool netTool = new NetTool();
        netTool.getNetData(URLValues.DANPIN, DanpinBean.class, new NetTool.NetListener<DanpinBean>() {
            @Override
            public void onSuccess(DanpinBean danpinBean) {
                leftAdapter.setDanpinBean(danpinBean);
                leftLv.setAdapter(leftAdapter);
                rightAdapter.setDanpinBean(danpinBean);
                rightLv.setAdapter(rightAdapter);

            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        /**
         * 右边的listview设置滑动监听
         */

        rightLv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                updateLeftListview(firstVisibleItem);
                leftLv.setSelection(firstVisibleItem);


            }
        });

        leftLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               // int firstVisibleItem = rightLv.getFirstVisiblePosition();
                updateLeftListview(position);
                rightLv.setSelection(position);
            }
        });

    }

    /**
     * 更新左边Listview字体颜色  并且更改主标题的内容
     */
    private void updateLeftListview(int firstVisibleItem) {

        TextView lasTextView = (TextView) leftLv.findViewWithTag(lastPosition);
        Log.d("MainActivity", "lasTextView:" + lasTextView);
        //在右边Listview第一次加载过程中会一直调用监听中的onscroll  这时的textView可能为空
        if (lasTextView != null) {
            lasTextView.setTextColor(Color.BLACK);//先将上一个textView字体的颜色改成黑色
            lasTextView.setBackgroundColor(0XFFF5F2F2);
        }
        TextView currenTextView = (TextView) leftLv.findViewWithTag(firstVisibleItem);
        //再将当前要改变的TextView字体的颜色改成红色
        if (currenTextView != null) {
            currenTextView.setTextColor(Color.RED);
            currenTextView.setBackgroundColor(0XFFFFFFFF);
        }
        lastPosition = firstVisibleItem;





    }


}
